/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-04-23 11:09:41
 * @LastEditors: huangtianyang
 * @LastEditTime: 2023-08-31 15:09:49
 * @FilePath: \cxl-admin\src\pages\operation\notice\list\index.ts
 */

import { noticeDelete, noticePage, noticePageInterface, noticeRelease } from '@/apis/operation/notice'
import router from '@/router'
import { BaseData, BaseMethod } from '@/utils/basc-data'
import { ElMessage, ElMessageBox } from 'element-plus'
import { reactive } from 'vue'
import * as tableConfig from './config.json'

export class Notice extends BaseData implements BaseMethod {
    //搜索数据
    searchData = reactive({
        date: '',
        type: '-1',
        status: '-1',
        title: ''
    })
    //公告消息弹窗数据
    noticeDialogData = reactive({
        visible: false,
        title: '公告消息',
        noticeId: ''
    })
    constructor() {
        super(tableConfig)

        this.getData()
    }

    isClick = false
    onOperation = (arg?: any) => {
        const { type, data } = arg
        if (!this.isClick) {
            this.isClick = true
            if (type === 'publish') {
                ElMessageBox.confirm(`确定要发布【${data.title}】的消息通知？`, '发布', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消'
                })
                    .then(() => {
                        this.isClick = false
                        noticeRelease(data.id, 1).then(res => {
                            if (res.data.value === 200) {
                                ElMessage.success('发布成功！')
                            }
                            this.getData()
                        })
                    })
                    .catch(() => {
                        this.isClick = false
                    })
            } else if (type === 'edit') {
                router.push({
                    path: '/operation/notice/edit',
                    query: { type: 'edit', id: data.id }
                })
            } else if (type === 'look') {
                this.noticeDialogData.visible = true
                this.noticeDialogData.noticeId = data.id
            } else if (type === 'delete') {
                ElMessageBox.confirm(`确定要删除【${data.title}】的消息通知？`, '删除', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消'
                })
                    .then(() => {
                        this.isClick = false
                        noticeDelete(data.id).then(res => {
                            if (res.data.value === 200) {
                                ElMessage.success('删除成功！')
                            }
                            this.getData()
                        })
                    })
                    .catch(() => {
                        this.isClick = false
                    })
            } else if (type === 'revocation') {
                ElMessageBox.confirm(`确定要撤销【${data.title}】的消息通知？`, '撤销', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消'
                })
                    .then(() => {
                        this.isClick = false
                        noticeRelease(data.id, 2).then(res => {
                            if (res.data.value === 200) {
                                ElMessage.success('撤销成功！')
                            }
                            this.getData()
                        })
                    })
                    .catch(() => {
                        this.isClick = false
                    })
            }
        }
        if (type === 'create') {
            router.push({ path: '/operation/notice/edit', query: { type } })
        }
    }
    /**
     * 拉取数据
     */
    getData = () => {
        const obj: noticePageInterface = {
            current: this.pageData.pagination.pageNo,
            size: this.pageData.pagination.pageSize,
            title: this.searchData.title
        }
        console.log(this.searchData.date)
        if (this.searchData.date) {
            console.log(this.searchData.date)
            obj.releaseStartTime = this.searchData.date[0] + ' 00:00:00'
            obj.releaseEndTime = this.searchData.date[1] + ' 23:59:59'
        }
        if (this.searchData.type != '-1' && this.searchData.type != '') {
            obj.type = Number(this.searchData.type)
        }
        if (this.searchData.status != '-1') {
            obj.releaseStatus = this.searchData.status
        }
        noticePage(obj).then(res => {
            if (res.data.value === 200) {
                this.pageData.table.data.length = 0
                this.pageData.pagination.total = res.data.data.total
                res.data.data.records.forEach((item: any) => {
                    item.noticeType = item.type
                    this.pageData.table.data.push(item)
                })
            }
        })
    }
    /**
     * 监听搜索
     */
    onSearch = (res: any) => {
        this.searchData.date = res.date
        this.searchData.type = res.type
        this.searchData.status = res.status
        this.searchData.title = res.title
        this.pageData.pagination.pageNo = 1
        this.getData()
    }
    /**
     * 监听点击重置按钮
     */
    onReset = () => {
        this.searchData.date = ''
        this.searchData.type = '-1'
        this.searchData.status = '-1'
        this.searchData.title = ''
        this.pageData.pagination.pageNo = 1
        this.getData()
    }
    /**
     * 监听分页发生改变
     */
    onPaginationChange = (index: number) => {
        this.pageData.pagination.pageNo = index
        this.getData()
    }
    /**
     * 监听选择每页显示个数
     */
    onPaginationSize = (size: number) => {
        this.pageData.pagination.pageNo = 1
        this.pageData.pagination.pageSize = size
        this.getData()
    }
    /**
     * 监听公告消息弹窗隐藏
     */
    onNoticeDialogVisible = () => {
        this.noticeDialogData.visible = false
        this.isClick = false
    }
}
